package com.daodao.daodaoschedule.infrastructure.util;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;

/**
 * jackson工具类
 * @author mal
 * @date 2021-07-13 15:12
 */
public class JacksonUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(JacksonUtil.class);

    private static ObjectMapper objectMapper = new ObjectMapper();

    static {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        objectMapper.configure(JsonGenerator.Feature.WRITE_NUMBERS_AS_STRINGS, false);
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));


    }

    private JacksonUtil(){

    }

    /**
     * 将对象转化为JSON
     * @param obj
     * @return
     */
    public static String writeObj2Json(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            LOGGER.error("writeObj2JSON error", e);
        }
        return null;
    }

    /**
     * 将对象转化为JSON
     * @param obj
     * @return
     */
    public static String writeObj(Object obj) {
        try {
            return objectMapper.writeValueAsString(obj);
        } catch (Exception e) {
            LOGGER.error("writeObj2JSON error", e);
        }
        return "";
    }

    /**
     * 将对象转化为格式化的JSON
     * @param obj
     * @return
     */
    public static String writeObj2PrettyJSON(Object obj) {
        try {
            return objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
        } catch (Exception e) {
            LOGGER.error("writeObj2JSON error", e);
        }
        return null;
    }

    /**
     * 将json转为指定的对象
     * @param jsonData
     * @param c
     * @param <T>
     * @return
     */
    public static <T> T readJson2Object(String jsonData, Class<T> c) {
        if (StringUtils.isBlank(jsonData)) {
            return null;
        }

        try {
            return objectMapper.readValue(jsonData, c);
        } catch (Exception e) {
            LOGGER.error("readJson2Object error", e);
        }
        return null;
    }

    /**
     * 将json转为指定的对象(泛型)
     * @param jsonData
     * @param typeReference
     * @param <T>
     * @return
     */
    public static <T> T readJson2Object(String jsonData, TypeReference<T> typeReference) {

        if (StringUtils.isBlank(jsonData)) {
            return null;
        }
        try {
            return objectMapper.readValue(jsonData, typeReference);
        } catch (Exception e) {
            LOGGER.error("readJson2Object error", e);
        }
        return null;
    }

    /**
     * 将json转为指定的对象(泛型)
     * @param jsonData
     * @param typeReference
     * @param <T>
     * @return
     */
    public static <T> T readJson2ObjectThrow(String jsonData, TypeReference<T> typeReference) throws IOException {

        if (StringUtils.isBlank(jsonData)) {
            return null;
        }
        return objectMapper.readValue(jsonData, typeReference);

    }




}
